﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Study.App_Code
{
    public class AsyncResult : IAsyncResult
    {
        // 标示异步处理的状态
        private bool isComplete = false;
        //保存异步处理程序中的Http上下文
        private HttpContext context;
        //异步回调的委托
        private AsyncCallback callback;
        /// <summary>
        /// 获取或设置保存下载文件的百分比数值部分
        /// </summary>
        public long PercentNumber;
        public AsyncResult(HttpContext context, AsyncCallback callback)
        {
            this.context = context;
            this.callback = callback;
        }
        /// <summary>
        /// 向客户端写入信息
        /// </summary>
        public void Send()
        {
            this.context.Response.Write(PercentNumber);
        }
        /// <summary>
        /// 完成异步处理，结束请求
        /// </summary>
        public void DoCompleteTask()
        {
            if (callback != null)
                callback(this);//会触发处理程序中的EndProcessRequest函数，结束请求
            this.isComplete = true;
        }
        #region IAsyncResult 成员
        public object AsyncState  //获取用户定义的对象，它限定或包含关于异步操作的信息
        {
            get { return null; }
        }
        public System.Threading.WaitHandle AsyncWaitHandle //获取用于等待异步操作完成的 WaitHandle
        {
            get { return null; }
        }
        public bool CompletedSynchronously //获取异步操作是否同步完成的指示
        {
            get { return false; }
        }
        public bool IsCompleted //获取异步操作是否已完成的指示
        {
            get { return isComplete; }
        }
        #endregion
    }
}